package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.FamilyManagementAdapter;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.SlideListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.FamilyManagementModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/2.
 * 户型管理
 */

public class FamilyManagementActivity extends Activity implements View.OnClickListener , Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.bottom_options)//添加分类
            LinearLayout bottom_options;
    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;
    Context context;
    @Bind(R.id.listview)
    SlideListView listview;

    FamilyManagementAdapter adapter;

    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取数据 1、下架 2、删除
    AlertDialog alertDialog;
    String id="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_management);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        bottom_options.setOnClickListener(this);

        if (getIntent()!=null){
            id=getIntent().getStringExtra("id");
        }

        adapter=new FamilyManagementAdapter(this);
        listview.setAdapter(adapter);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.bottom_options:
                Intent intent=   new Intent(context,AddFamilyActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
                break;


        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        processinitData();
    }
    //删除
    public void processdeleting(final String id,final String img) {
        alertDialog = Dialog.phonedialog(context, "是否删除该户型", "删除户型", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Sundry.isOpenNetwork(context)) {
                    mDialog = new MyProgressDialog(context);
                    Judge_interface = "1";
                    HttpClient.delhometype(id,img,FamilyManagementActivity.this);
                } else {
                    ToastUtils.Toastshow(context, getResources().getString(R.string.network));
                }
                alertDialog.dismiss();
            }
        }, "确定", "取消");
    }

    //初始数据
    private void processinitData() {
        if (Sundry.isOpenNetwork(context)) {
            if (Judge_interface.equals("0")){
                mDialog = new MyProgressDialog(context);
            }
            Judge_interface = "0";
           HttpClient.hometype(id, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
    if (mDialog != null) {
        mDialog.dismiss();
    }

        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
        String error = getnote.get(0).getError();
        String mssage = getnote.get(0).getMssage();
            if (Judge_interface.equals("0")) {//数据
            adapter.adddeleting();
            }
        if (error.equals("200")) {
            String content = getnote.get(0).getResult();
            if (Judge_interface.equals("0")) {//数据
                available_layout.setVisibility(View.GONE);
                List<FamilyManagementModel> gethometype = JsonTools.gethometype(content);
                if (gethometype != null || gethometype.size() > 0) {
                    adapter.addItem(gethometype);
                }
            } else if (Judge_interface.equals("1")) {//删除
                ToastUtils.show(context, mssage);
                processinitData();
            }
        } else if (error.equals("105")) {
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
            ToastUtils.show(context, mssage);
        } else {
            if (Judge_interface.equals("0")) {//数据
                available_layout.setVisibility(View.VISIBLE);
            }
            ToastUtils.show(context, mssage);
        }
    }

}

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {//数据
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));

    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {//数据
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
